package gao.xiaolei.service;

import org.springframework.stereotype.Service;
import gao.xiaolei.dao.TimeSheetDao;
import gao.xiaolei.entity.TimeSheet;
import gao.xiaolei.exception.CheckOnWorkException;
import gao.xiaolei.util.UuidUtil;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service("TimeSheetService")
public class TimeSheetService {

    @Resource(name = "TimeSheetDao")
    private TimeSheetDao timeSheetDao;

    //在某个项目打卡
    @Transactional
    public Integer checkOoWorkOnProject(String userId,String projectId){
        int hasCheckTime=timeSheetDao.countAllByProjectAndUserIdAndCheckTimeAfterAndCheckTimeBefore(projectId,userId, LocalDateTime.of(LocalDate.now(), LocalTime.of(0,0,0)),LocalDateTime.of(LocalDate.now(), LocalTime.of(23,59,59)));
        if(hasCheckTime>=5)
            throw new CheckOnWorkException("每个项目每天的打开次数不能超过5次！");
        else
            timeSheetDao.save(new TimeSheet(UuidUtil.get32Uuid(),userId,LocalDateTime.now(),projectId));
        return hasCheckTime+1;
    }

    //获取某个人在某个项目今天打卡了多少次
    public Integer getCheckTimeToday(String userId,String projectId){
        return timeSheetDao.countAllByProjectAndUserIdAndCheckTimeAfterAndCheckTimeBefore(projectId,userId, LocalDateTime.of(LocalDate.now(), LocalTime.of(0,0,0)),LocalDateTime.of(LocalDate.now(), LocalTime.of(23,59,59)));
    }
}
